package com.pet.serve.repository;

import com.pet.serve.entity.Customers;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Optional;


@Repository
public interface CustomerRepository extends JpaRepository<Customers, Long> {

    /**
     * 分页获取所有客户
     *
     * @param query 过滤条件
     * @param pageable 分页参数
     * @return 分页的客户列表
     */
    @Query("SELECT c FROM Customers c WHERE c.username LIKE %:query% OR c.nickname LIKE %:query%")
    Page<Customers> findAllByQuery(String query, Pageable pageable);


    /**
     * 根据 ID 查找客户
     *
     * @param id 客户 ID
     * @return 客户对象
     */
    Optional<Customers> findById(Long id);

    /**
     * 按用户名查找客户
     *
     * @param username 用户名
     * @return 客户对象
     */
    Optional<Customers> findByUsername(String username);

    /**
     * 删除客户
     *
     * @param id 客户 ID
     */
    void deleteById(Long id);

    /**
     * 批量删除客户
     *
     * @param usernames 用户名列表
     */
    @Modifying
    @Query("DELETE FROM Customers c WHERE c.username IN :usernames")
    void deleteBatch(@Param("usernames") List<String> usernames);

    /**
     * 按权限查找客户
     *
     * @param permission 权限
     * @return 客户列表
     */
    List<Customers> findByPermission(Customers.UserRole permission);

    /**
     * 通过等级过滤 VIP 客户
     *
     * @param level 客户等级
     * @return 客户列表
     */
    Page<Customers> findByLevel(String level, Pageable pageable);


    Page<Customers> findByLevelContaining(String vip, org.springframework.data.domain.Pageable pageable);

    Page<Customers> findAll(Pageable pageable);


    @Query("SELECT c FROM Customers c WHERE c.name LIKE %:name%")
    List<Customers> findByName(@Param("name") String name, Pageable pageable);


    List<Customers> findByPermission(Customers.UserRole permisson, PageRequest of);

    List<Customers> findByRegdayBetween(Date regdayStart, Date regdayEnd, Pageable pageable);

    List<Customers> findByBirthdayBetween(Date birthdayStart, Date birthdayEnd, Pageable pageable);
}
